<template>
	<div>
		<!--面包屑-->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>公告管理</el-breadcrumb-item>
			<el-breadcrumb-item>公告列表</el-breadcrumb-item>
		</el-breadcrumb>

		<!--内容卡片-->
		<el-card class="box-card">
			<!--搜索框-->
			<el-row :gutter="20">
				<el-col :span="5">
					<!--模糊搜索框-->
					<el-input placeholder="请输入查询标题名称" clearable="" @clear="select" v-model="queryAffiche.title"
						class="input-with-select">
						<el-button slot="append" icon="el-icon-search" @click="select"></el-button>
					</el-input>
				</el-col>
				
				<el-col :span="4">
					<!--添加公告按钮-->
					<el-button type="primary" @click="addDialogVisible=true">添加公告</el-button>
				</el-col>
			</el-row>


			<!-- 表格数据  :data代表表数据  border：带有四边框-->
			<el-table :data="affiches" border style="width: 100%;">
				<!-- prop：表格当前列的字段名称-->
				<el-table-column type="index" label=""></el-table-column>
				<el-table-column prop="title" width="150px" align="center" label="标题"></el-table-column>
				<el-table-column prop="message" align="center" label="内容"></el-table-column>
				<el-table-column prop="beginDate" width="160px" align="center" label="开始日期"></el-table-column>
				<el-table-column prop="endDate" align="center" width="160px" label="结束日期"></el-table-column>

				
				<el-table-column label="操作" width="150" align="center">
					<template slot-scope="scope">
						<el-tooltip class="item" :enterable="false" effect="dark" content="修改" placement="top">
							<el-button @click="toupdate(scope.row.id)" type="primary" icon="el-icon-edit" size="mini">
							</el-button>&nbsp;&nbsp;&nbsp;&nbsp;
						</el-tooltip>
						<el-tooltip class="item" :enterable="false" effect="dark" content="删除" placement="top">
							<el-button @click="deleteAffiche(scope.row.id)" type="danger" icon="el-icon-delete"
								size="mini">
							</el-button>
						</el-tooltip>
					</template>
				</el-table-column>
			</el-table>

			<!--分页
					@size-change:当每页显示的条数改变时触发函数
					@current-change：当前页码的改变触发的函数
					current-page：当前页码
					page-sizes：每页显示的条数的数组
					page-size：默认每页显示的条数
					-->
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryAffiche.pageNum" :page-sizes="[1,3,5,10]" :page-size="queryAffiche.pageSize"
				layout="total, sizes, prev, pager, next, jumper" :total="total" style="padding:25px 350px;">
			</el-pagination>
		</el-card>
		
		
		
		<!--添加用户信息的对话框
			   title：对话框的提示语句
			   addDialogVisible:当前的对话框是否显示
			   width：宽带的百分比
			-->
		<el-dialog title="添加公告信息" :visible.sync="addDialogVisible" width="50%" :before-close="reset">
			<!-- 添加用户信息的表单 -->
			<el-form :model="addAffiche" ref="addAfficheRef" :rules="addAffiches" label-width="80px">
				<el-form-item label="标题" prop="title">
					<el-input v-model="addAffiche.title"></el-input>
				</el-form-item>
				<el-form-item label="公告内容" prop="message">
					<el-input type="textarea" v-model="addAffiche.message"></el-input>
				</el-form-item>
				<el-form-item label="开始日期" prop="beginDate">
					<el-input type="date" v-model="addAffiche.beginDate"></el-input>
				</el-form-item>
				<el-form-item label="结束日期" prop="endDate">
					<el-input type="date" v-model="addAffiche.endDate"></el-input>
				</el-form-item>
			</el-form>
			<!-- 取消和添加按钮 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="reset">取 消</el-button>&nbsp;
				<el-button type="primary" @click="addAfficheForm">确 定</el-button>
			</span>
		</el-dialog>
		
		
		
		
		
		<!--修改的对话框-->
		<el-dialog title="修改公告信息" :visible.sync="updateDialogVisible" width="50%" :before-close="updateReset">
			<!-- 修改用户信息的表单 -->
			<el-form :model="updateAffiche" ref="updateAfficheRef" :rules="addAffiches" label-width="80px">
				<el-form-item label="标题" prop="title">
					<el-input v-model="updateAffiche.title"></el-input>
				</el-form-item>
				<el-form-item label="公告内容" prop="message">
					<el-input type="textarea" v-model="updateAffiche.message"></el-input>
				</el-form-item>
				<el-form-item label="开始日期" prop="beginDate">
					<el-input type="date" v-model="updateAffiche.beginDate"></el-input>
				</el-form-item>
				<el-form-item label="结束日期" prop="endDate">
					<el-input type="date" v-model="updateAffiche.endDate"></el-input>
				</el-form-item>
			</el-form>
			<!-- 取消和添加按钮 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="updateReset">取 消</el-button>&nbsp;
				<el-button type="primary" @click="updateAfficheForm">修 改</el-button>
			</span>
		</el-dialog>





	</div>
</template>

<script>
	import qs from 'qs'
	export default {
		data() {
			return {
				//模糊分页查询对象
				queryAffiche: {
					title: '',
					roleId:1,
					pageNum: 1,
					pageSize: 5
				},
				//权限列表集合
				affiches: [],
				//总数居条数
				total: 0,
				addDialogVisible: false,
				addAffiche: {
					title:'',
					message:'',
					beginDate:'',
					endDate:''
				},
				//添加数据的表单验证
				addAffiches: {
					title: [{
							required: true,
							message: '请输入公告标题',
							trigger: 'blur'
						},
						{
							min: 2,
							max: 20,
							message: '长度在 2 到 20 个字符',
							trigger: 'blur'
						}
					],
					message: [{
							required: true,
							message: '请输入公告内容',
							trigger: 'blur'
						},
						{
							min: 6,
							message: '长度最少 6 个字符',
							trigger: 'blur'
						}
					]
				},
				//修改对话框
				updateDialogVisible: false,
				updateAffiche: {
					id: '',
					title:'',
					message:'',
					beginDate:'',
					endDate:''
				}
			}
		},
		methods: {
			//模糊分页查询
			async getAffiche() {
				this.users=[];
				var res = await this.$http.post("selectAllAffiche", qs.stringify(this.queryAffiche))
				if (res.data.code == 200) {
					this.$message.success(res.data.message)
					//查询数据赋值给powers
					this.affiches = res.data.data.records;
					//获取数据的总条数
					this.total = res.data.data.total;
				} else {
					this.$message.error(res.data.message)
				}
			},
			//改变每页显示的条数
			handleSizeChange(newpageSize) {
				this.queryAffiche.pageSize = newpageSize;
				this.getAffiche();
			},
			//当前页码改变时触发的函数
			handleCurrentChange(newpageNum) {
				this.queryAffiche.pageNum = newpageNum;
				this.getAffiche();
			},
			//根据权限名模糊查询
			select() {
				this.queryAffiche.pageNum = 1;
				this.getAffiche();
			},
			//添加对话框中的取消事件
			reset() {
				this.addDialogVisible = false; //关闭对话框
				this.$refs.addAfficheRef.resetFields();
			},
			//添加数据的确认按钮
			addAfficheForm() {
				this.$refs.addAfficheRef.validate(async (bo) => {
					if (bo) { //验证通过时 调用
						//调用后端的添加数据的接口，进行添加即可	
						var res = await this.$http.post("addAffiche", qs.stringify(this.addAffiche))
						if (res.data.code == 200) { //成功
							this.$message.success(res.data.message)
							//更新页码数据
							this.getAffiche();
						} else { //失败
							this.$message.error(res.data.message)
						}
						//清空数据
						this.addAffiche = {}
						this.addDialogVisible = false; //关闭对话框
					} else {
						this.$message.error("数据不合法，请检查！")
					}
				})
			},
			//删除用户数据
			deleteAffiche(id) {
				this.$confirm('删除数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async () => {
					var res = await this.$http.get("deleteAffiche?id=" + id)
					if (res.data.code == 200) {
						this.$message.success(res.data.message);
					} else {
						this.$message.error(res.data.message);
					}
					this.getAffiche();
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			//根据id展示
			async toupdate(id) {
				//从后端根据id来查询当前用户的信息
				var res = await this.$http.get("selectAfficheByID?id=" + id);
				if (res.data.code == 200) {
					this.updateAffiche = res.data.data; //赋予对象
					this.updateDialogVisible = true; //展示修改的对话框
				} else {
					this.$message.error(res.data.message)
				}
			},
			//修改对话框中的取消事件
			updateReset() {
				this.updateDialogVisible = false; //关闭对话框
				this.$refs.updateAfficheRef.resetFields();
			},
			//修改数据的确认按钮
			updateAfficheForm() {
				this.$refs.updateAfficheRef.validate(async (bo) => {
					if (bo) { //验证通过时 调用
						var res = await this.$http.post("updateAffiche", qs.stringify(this.updateAffiche))
						if (res.data.code == 200) { //成功
							this.$message.success(res.data.message),
								//更新页码数据
								this.getAffiche();
						} else { //失败
							this.$message.error(res.data.message)
						}
						this.updateDialogVisible = false; //关闭对话框
					} else {
						this.$message.error("数据不合法，请检查！")
					}
			
				})
			}
		},
		//当前页码加载前执行的代码
		mounted() {
			this.getAffiche()
		}
	}
</script>

<style scoped>
	.el-breadcrumb {
		font-size: 16px !important;
		margin: 15px 8px;
		margin-bottom: 20px;
	}
	/deep/.el-table__body-wrapper{
		color:coral;
	}
</style>
